﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.InboundServiceRef;
    using WMS6.Web.InventoryMgtServiceRef;

    public class Adjustment : BasePage
    {
        protected Button btnAddDetail;
        protected Button btnDeleteDetail;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnRefresh;
        protected Button btnSave;
        protected HtmlForm F5031;
        protected GridView gvadjustmentDetail;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLineID;
        protected HtmlInputHidden hdnReceiptID;
        protected HtmlHead Head1;
        protected HtmlInputHidden hidCheck;
        protected HtmlImage img;
        protected HtmlImage imgTime;
        protected Label lblAdJustMentDate;
        protected Label lblAdJustMentID;
        protected Label lblASN;
        protected Label lblCreatedBy;
        protected Label lblMsg;
        protected Label lblOwnerID;
        protected Label lblRemark;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        protected TextBox txtAdJustMentDate;
        protected TextBox txtAdJustMentID;
        protected TextBox txtCreatedBy;
        protected TextBox txtOwnerID;
        protected TextBox txtRemark;
        protected UpdatePanel up1;

        protected void btnAddDetail_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtOwnerID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5150_003");
            }
            else if (string.IsNullOrEmpty(this.txtAdJustMentDate.Text))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5150_004");
            }
            else
            {
                if (string.IsNullOrEmpty(this.hdnReceiptID.Value))
                {
                    this.InsertAdjustmentInfo();
                }
                base.Response.Redirect("AdjustmentDetail.aspx?ID=" + base.UrlEncode(this.hdnReceiptID.Value) + "&LienID=" + base.UrlEncode(this.hdnLineID.Value) + "&OwnerID=" + base.UrlEncode(this.txtOwnerID.Text), true);
            }
        }

        protected void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            List<string> lineID = base.GetSelectedIDsInGridView(this.gvadjustmentDetail, "LineID");
            try
            {
                this.lblMsg.Text = InventoryMgtServiceClientProxy.GetInstance().DeleteAdJustMentDetail(this.txtAdJustMentID.Text, lineID) + base.GetGlobalMsgResource("C006");
                this.SetAdJustMentInfo2Form();
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C012") + ex.Message;
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("AdjustmentQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("AdjustmentList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            this.txtAdJustMentID.Text = string.Empty;
            this.txtAdJustMentDate.Text = string.Empty;
            this.txtOwnerID.Text = string.Empty;
            this.txtOwnerID.Enabled = true;
            this.lblMsg.Text = "";
            this.hdnFlag.Value = "0";
            this.hdnReceiptID.Value = "";
            this.btnSave.Enabled = true;
            this.img.Disabled = false;
            this.imgTime.Disabled = false;
            this.txtAdJustMentDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            this.txtRemark.Text = "";
            this.txtRemark.Enabled = true;
            base.BindData2GridView(this.gvadjustmentDetail, new List<WMS6.Web.InboundServiceRef.AdjustmentDetailInfo>());
            this.txtEnabled();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtAdJustMentDate.Text))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5150_002");
                this.lblMsg.Visible = true;
            }
            else if (string.IsNullOrEmpty(this.txtOwnerID.Text))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5150_003");
                this.lblMsg.Visible = true;
            }
            else
            {
                try
                {
                    if (string.IsNullOrEmpty(this.hdnFlag.Value) || (this.hdnFlag.Value == "0"))
                    {
                        this.InsertAdjustmentInfo();
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                    this.lblMsg.Visible = true;
                }
            }
        }

        protected string FormatAdjustmentUrl(string url, string adjumstId, string LineID)
        {
            return string.Format(url, this.hdnFrom.Value.Trim(), adjumstId, LineID);
        }

        private WMS6.Web.InventoryMgtServiceRef.AdjustmentInfo GetAdjustmentInfo()
        {
            return new WMS6.Web.InventoryMgtServiceRef.AdjustmentInfo { AdjustmentDate = DateTime.Parse(this.txtAdJustMentDate.Text), OwnerID = this.txtOwnerID.Text, CreatedBy = base.UserInfo.UserID, UpdatedBy = base.UserInfo.UserID, IsActive = "Y", Remark = this.txtRemark.Text };
        }

        protected void gvadjustmentDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F5031", this.gvadjustmentDetail);
        }

        protected void gvadjustmentDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.PageQueryEntitySession as WMS6.Web.InventoryMgtServiceRef.AdjustmentInfo).AdjustmentDetail;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
        }

        protected void gvadjustmentDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
                base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
            }
        }

        protected void gvadjustmentDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                WMS6.Web.InboundServiceRef.AdjustmentInfo adjustmentInfo = base.PageQueryEntitySession as WMS6.Web.InboundServiceRef.AdjustmentInfo;
                DataView dv = CADataConverter.ToDataTable(adjustmentInfo.AdjustmentDetail).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvadjustmentDetail, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        private void InsertAdjustmentInfo()
        {
            try
            {
                string strAdjumentID = string.Empty;
                strAdjumentID = InventoryMgtServiceClientProxy.GetInstance().InsertAdJustMent(this.GetAdjustmentInfo());
                if (!string.IsNullOrEmpty(strAdjumentID))
                {
                    this.hdnReceiptID.Value = strAdjumentID;
                    this.lblMsg.Text = strAdjumentID + base.GetGlobalMsgResource("C004");
                    this.lblMsg.Visible = true;
                    this.txtAdJustMentID.Text = strAdjumentID;
                    this.SetAdJustMentInfo2Form();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnReceiptID.Value = base.Request.QueryString["ID"].ToString();
                this.hdnFlag.Value = "1";
                this.SetAdJustMentInfo2Form();
                this.img.Disabled = true;
                this.imgTime.Disabled = true;
            }
            else
            {
                this.txtCreatedBy.Text = base.UserInfo.UserID;
                this.txtAdJustMentDate.Text = base.GetPreferenceShortDate(DateTime.Now);
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!(string.IsNullOrEmpty(base.Request.QueryString["detail"]) || !(base.Request.QueryString["detail"].ToString() == "1")))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
                this.txtEnabled();
            }
        }

        private void SetAdJustMentInfo2Form()
        {
            try
            {
                string adjustmentID = this.hdnReceiptID.Value.ToString();
                WMS6.Web.InboundServiceRef.AdjustmentInfo adjustmentInfo = InboundServiceClientProxy.GetInstance().GetAdjustmentInfoByID(adjustmentID);
                base.PageQueryEntitySession = adjustmentInfo;
                if (adjustmentInfo != null)
                {
                    this.txtAdJustMentID.Text = adjustmentInfo.AdjustmentID;
                    this.txtAdJustMentDate.Text = base.GetPreferenceShortDate(adjustmentInfo.AdjustmentDate);
                    this.txtOwnerID.Text = adjustmentInfo.OwnerID;
                    this.txtCreatedBy.Text = adjustmentInfo.CreatedBy;
                    this.txtRemark.Text = adjustmentInfo.Remark;
                    this.txtRemark.Enabled = false;
                    if ((adjustmentInfo.AdjustmentDetail != null) && (adjustmentInfo.AdjustmentDetail.Count > 0))
                    {
                        this.hdnLineID.Value = (adjustmentInfo.AdjustmentDetail[adjustmentInfo.AdjustmentDetail.Count - 1].LineID + 10).ToString();
                    }
                    else
                    {
                        this.hdnLineID.Value = "10";
                    }
                    base.BindData2GridView(this.gvadjustmentDetail, adjustmentInfo.AdjustmentDetail);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void txtEnabled()
        {
            string CS40000 = this.hdnFlag.Value;
            if (CS40000 != null)
            {
                if (!(CS40000 == "") && !(CS40000 == "0"))
                {
                    if (CS40000 == "1")
                    {
                        this.txtAdJustMentID.Enabled = false;
                        this.txtAdJustMentDate.Enabled = false;
                        this.txtOwnerID.Enabled = false;
                    }
                }
                else
                {
                    this.txtAdJustMentID.Text = "系统自动生成";
                    this.txtAdJustMentID.Enabled = false;
                    this.txtAdJustMentDate.Enabled = true;
                }
            }
        }
    }
}

